package com.zhengjiang.pattern.demo.strategy;

import java.util.Scanner;

/**
 * 策略调用者
 */
public class Caculator {

    private Operation operation;
    public void caculate(int a,int b){
        operation.doOperate(a,b);
    }

    public Caculator setOperation(Operation operation){
        this.operation = operation;
        return this;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag){
            System.out.println("请输入计算方式：Add-加法 Del-减法 Multi-乘法 0-退出");
            String input = scanner.next();
            try {
                Class clazz = Class.forName("com.zhengjiang.pattern.demo.strategy.Operation"+input);
                Operation instance = (Operation) clazz.newInstance();
                new Caculator().setOperation(instance).caculate(1,2);
            } catch (Exception e) {
                flag = false;
            }
        }
    }
}
